package com.qf.ssm.controller;
import com.qf.ssm.ex.ControllerException;
import com.qf.ssm.mapper.CarRentalCertificateMapper;
import com.qf.ssm.po.CarReturnInformation;
import com.qf.ssm.qo.CarRentalCertificateQo;
import com.qf.ssm.qo.PickUpInformationQo;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarRentalCertificateService;
import com.qf.ssm.utlis.FilesUtils;
import com.qf.ssm.vo.CarReturnInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 黄梓濠
 * @Create 2024-01-05-15:56
 */
@RestController
@RequestMapping("/carreturninformation")
@Api(tags = "还车模块")
public class CarReturnInformationController {
    @Autowired
    private CarRentalCertificateService carRentalCertificateService;
    @Autowired
    private CarRentalCertificateMapper carRentalCertificateMapper;
    /**
     *
     * @param orderId 订单主键
     * @return
     */
    @ApiOperation("获取还车订单原有的数据")
    @PostMapping("/get/carrental")
    public ResponseResult<CarReturnInformationVo> getCarRental(Integer orderId) {
        CarReturnInformationVo carRental = carRentalCertificateService.getCarRental(orderId);
        return ResponseResult.success(carRental);
    }

    @ApiOperation("还车订单的添加并将订单状态改为已完成")
    @PostMapping("finish")
    public ResponseResult<Integer> getCarRentalFinish(@RequestBody CarRentalCertificateQo carRentalCertificateQo) {
        if (ObjectUtils.isEmpty(carRentalCertificateQo)){
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
        Integer count = carRentalCertificateService.carRentalFinish(carRentalCertificateQo);
        return ResponseResult.success(count);

    }


    /**
     *
     * @param request
     * @param files 上传的文件数组
     * @param returnId 还车表主键
     * @return
     */
    //图片
    @ApiOperation("还车时添加的图片")
    @PostMapping("/upload/pictures")
    public ResponseResult<Integer> uploadPictures(HttpServletRequest request, MultipartFile[] files, @RequestParam("returnId") Integer returnId){
        //图片上传
        String filenames = null;
        try {
            filenames = FilesUtils.fileUpload(request, files);
        } catch (IOException e) {
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
       CarRentalCertificateQo carRentalCertificateQo = new CarRentalCertificateQo();
        carRentalCertificateQo.setPhoto(filenames);
        carRentalCertificateQo.setReturnId(returnId);
        System.out.println(carRentalCertificateQo);
        Integer count = carRentalCertificateMapper.updateCarRentalCertificate(carRentalCertificateQo);
        return ResponseResult.success(count);
    }
}
